<template>
  <div class="login-main">
    <el-container>
      <el-header class="">
        <img class="m-logo" src="http://cas.swust.edu.cn/authserver/images/logoImg.png" alt="">
      </el-header>
      <el-main>
        <div class="ms-login">
          <div class="ms-title editBan">毕业生信息管理系统</div>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
            <el-form-item prop="用户名">
              <el-input v-model="ruleForm.userName" placeholder="username">
                <template #prepend>
                  <el-button icon="el-icon-user"></el-button>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="密码">
              <el-input
                  type="password"
                  placeholder="password"
                  v-model="ruleForm.password"
                  @keyup.enter="submitForm()"
              >
                <template #prepend>
                  <el-button icon="el-icon-lock"></el-button>
                </template>
              </el-input>
            </el-form-item>
            <div class="login-btn">
              <el-form-item>
                <el-button type="primary" @click="submitForm()">登录</el-button>
              </el-form-item>
            </div>
            <p class="login-tips">Tips :无账户密码请联系管理员 </p>
          </el-form>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Login.vue",
  data() {
    return {
      ruleForm: {
        userName: '',
        password: ''
      },
      rules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'change'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'change'}
        ]
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const _this = this
          // alert('submit!');
          this.$axios.post('http://localhost:9926/login', this.ruleForm).then(res => {

            const jwt = res.headers['authorization']
            const userInfo = res.data.data
            _this.$common.objectValueAtoB(userInfo, _this.$common.userInfo)
            _this.$store.commit("SET_TOKEN", jwt)
            _this.$store.commit("SET_USERINFO", userInfo)
            if (res.data.data.roles === 'student') {
              _this.$set(_this.$common.status, 'student', true)
            }
            _this.$router.push('/graduates')
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const _this = this
    let jwt = this.$store.state.token
    this.$axios.get('/loginCheck?jwt=' + jwt).then((res) => {
      if (res.data.code === 200 && _this.$store.state.userInfo !== null) {
        _this.$router.push("/graduates");
      } else {
        if (_this.$store.state.userInfo === null) {
          console.log("登录已过期");
        } else {
        }
      }
    });
  }
}
</script>

<style scoped>

.editBan {
  -moz-user-select: none; /*火狐*/
  -webkit-user-select: none; /*webkit浏览器*/
  -ms-user-select: none; /*IE10*/
  -khtml-user-select: none; /*早期浏览器*/
  -o-user-select: none; /* Opera*/
}

.el-header {
  /*background-color: #B3C0D1;*/
  background-color: rgba(155, 155, 155, 0.6);
  position: relative;
  color: #333;
  text-align: center;
  line-height: 40px;
  height: 200px;
}

.el-main {
  height: 100%;
}

.login-main {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(../assets/img/login-bg.jpg);
  background-size: 100% 100%;
  background-repeat: no-repeat;


  margin: 0;
  padding: 0;

}

.m-logo {
  height: 100%;
  /*margin-top: 10px;*/
}

.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #183a65;
  border-bottom: 1px solid #ddd;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}

.ms-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}

.login-tips {
  text-align: left;
  font-size: 12px;
  line-height: 30px;
  color: #183a65;
  margin: 0;
}
</style>